package edu.uestc.gene.ui.util;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

/**
 * 面板绘制相关工具类
 * 
 * @author CarlCheng
 * 
 */

public class ShellUtil {
	/**
	 * griddata样式1:横向填充所有空间
	 * 
	 * @param horizontalSpan
	 *            横向占用格数
	 * @param verticalSpan
	 *            纵向占用格数
	 * @return
	 */
	public static GridData getData1(int horizontalSpan, int verticalSpan) {
		GridData data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.CENTER;
		data.horizontalSpan = horizontalSpan;
		data.verticalSpan = verticalSpan;
		data.grabExcessHorizontalSpace = true;
		return data;
	}

	/**
	 * griddata样式3,不填满空间
	 * 
	 * @param horizontalSpan
	 *            横向占用格数
	 * @param verticalSpan
	 *            纵向占用格数
	 * @return
	 */
	public static GridData getData3(int horizontalSpan, int verticalSpan) {
		GridData data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.CENTER;
		data.horizontalSpan = horizontalSpan;
		data.verticalSpan = verticalSpan;
		return data;
	}

	/**
	 * griddata样式2:横向纵向填充所有空间
	 * 
	 * @param horizontalSpan
	 *            横向占用格数
	 * @param verticalSpan
	 *            纵向占用格数
	 * @return
	 */
	public static GridData getData2(int horizontalSpan, int verticalSpan) {
		GridData data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.FILL;
		data.horizontalSpan = horizontalSpan;
		data.verticalSpan = verticalSpan;
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = true;
		return data;
	}

	/**
	 * 打开MessageBox显示所需要的信息
	 * 
	 * @param title
	 *            提示信息
	 * @param message
	 *            提示消息
	 * @param style
	 *            消息盒子类型
	 * @return MessageBox.open()值
	 */
	public static int getMessage(Shell shell, String title, String message,
			int style) {
		MessageBox messageBox = new MessageBox(shell, style);
		messageBox.setMessage(message);
		messageBox.setText(title);
		return messageBox.open();
	}

	/**
	 * 得到FontData对象以设置字体信息
	 * 
	 * @param height
	 *            字体大小
	 * @param style
	 *            字体风格
	 * @return
	 */
	public static Font getFont(int height, int style) {
		FontData data = new FontData();
		data.setStyle(style);
		data.height = height;
		Font font = new Font(Display.getCurrent(), data);
		return font;
	}

	/**
	 * 加载图片
	 * 
	 * @param path
	 * @return
	 */
	public static Image createImage(String path) {
		Image img = new Image(Display.getCurrent(),
				ShellUtil.class.getResourceAsStream(path));
		return img;
	}

	/**
	 * 计算使shell显示在屏幕中间的坐标
	 * 
	 * @param shell
	 * @param width
	 *            shell宽
	 * @param height
	 *            shell高
	 * @return
	 */
	public static Rectangle setLocation(Shell shell, int width, int height) {
		Monitor primary = Display.getCurrent().getPrimaryMonitor();// 获取监视器类
		Rectangle bounds = primary.getBounds();// 得到监视器分辨率
		// 计算使shell位于监视器中间的坐标
		int x = bounds.x + (bounds.width - width) / 2;
		int y = bounds.y + (bounds.height - height) / 2;
		return new Rectangle(x, y, width, height);
	}

	/**
	 * 设置dialog的位置大小，位于父面板的正中
	 * 
	 * @param parent
	 * @return
	 */
	public static Rectangle centerRect(Rectangle parent, int width, int height) {
		Rectangle rect = new Rectangle(parent.x - (400 - parent.width) / 2,
				parent.y + (parent.height - 300) / 2, width, height);
		return rect;
	}

	/**
	 * 使用默认浏览器打开指定网址
	 * 
	 * @param url
	 */
	public static void openDefaultBrowser(String url) {
		if (Desktop.isDesktopSupported()) {
			Desktop desktop = Desktop.getDesktop();
			try {
				desktop.browse(new URI(url));
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (URISyntaxException e1) {
				e1.printStackTrace();
			}
		} else {
			System.out.println("不支持desktop");
		}
	}

}
